package 初级算法8月开始.数组;
// https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/
public class 买卖股票的最佳时机122II0815 {
    class Solution {
        //动态规划
        public int maxProfit(int[] prices) {
            int n = prices.length;
            int dp0 = 0, dp1 = -prices[0];
            for (int i = 1; i < n; ++i) {
                int newDp0 = Math.max(dp0, dp1 + prices[i]);
                int newDp1 = Math.max(dp1, dp0 - prices[i]);
                dp0 = newDp0;
                dp1 = newDp1;
            }
            return dp0;
        }

        //贪心法
        // public int maxProfit(int[] prices) {
        //     int ans = 0;
        //     int n = prices.length;
        //     for (int i = 1; i < n; ++i) {
        //         ans += Math.max(0, prices[i] - prices[i - 1]);
        //     }
        //     return ans;
        // }



    }
    //https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/solution/javashi-pin-jiang-jie-xi-lie-best-time-to-buy-and-/
    public int maxProfit(int[] prices) {
        if(prices==null||prices.length==0)
            return  0;
        int sum=0;
        for(int i=1;i<prices.length;i++){
            int current=prices[i];
            int prev=prices[i-1];
            if(current>prev)
                sum+=current-prev;
        }
        return sum;
    }

    public static void main(String[] args) {
        System.out.println(new 买卖股票的最佳时机122II0815().maxProfit(new int[]{7,1,5,3,6,4}));

    }

}
